package com.leonzhangxf.web.reactive.webclient;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.reactive.ClientHttpConnector;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

/**
 * The demonstrate for the use of {@link org.springframework.web.reactive.function.client.WebClient}.
 *
 * @author leonzhangxf
 * @date 20190428
 */
@Configuration
public class WebClientStarter {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext =
            new AnnotationConfigApplicationContext(WebClientStarter.class);

        WebClient webClient = applicationContext.getBean(WebClient.class);
        Mono<String> mono = webClient.get()
            .uri("https://www.boxuegu.com/online/message/findMessageCount").exchange()
            .flatMap(clientResponse -> clientResponse.bodyToMono(String.class));
        System.out.println(mono.block());
    }

    @Bean
    public WebClient webClient(ClientHttpConnector clientHttpConnector) {
        WebClient.Builder builder = WebClient.builder();
        builder.clientConnector(clientHttpConnector);
        return builder.build();
    }

    @Bean
    public ReactorClientHttpConnector reactorClientHttpConnector() {
        return new ReactorClientHttpConnector();
    }
}
